package com.supervision.manage.service

import com.fly.base.core.Core
import com.fly.base.misc.uid.UIDRunner
import com.fly.base.misc.uid.UIDService
import com.fly.base.redis.RedisService
import com.fly.base.util.DateFormatUtil
import com.fly.base.util.Util
import com.supervision.manage.common.DXKey
import com.supervision.domain.model.User
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import org.springframework.stereotype.Service

/**
 *  Created by whis on 11/16/18
 */
@Service
@Component
open class BaseService {
    companion object {
        private val logger = LoggerFactory.getLogger(BaseService::class.java)
    }

    @Autowired lateinit var redisService: RedisService
    @Autowired lateinit var uidService: UIDService

    open fun makeUserUid(): String {
        return uidService.get(object : UIDRunner {
            override fun generate(): String {
                return Util.generateRandomString(8)
            }

            override fun exists(uid: String): Boolean? {
                return Core.Q().findByCol(User::class.java, "LOWER(uid)", uid.toLowerCase()) != null
            }

            override fun lockKey(uid: String): String {
                return DXKey.getKeyOfMakeUserUidLock(uid)
            }
        })
    }

    open fun makeOrderSn(): String {
        val time = DateFormatUtil.timeFormat(DateFormatUtil.time(), "yyyyMMdd")

        val hashCode = ((Math.random() * 9 + 1) * 100000000).toInt().toString()

        return time.substring(0, time.length) + hashCode
    }
}